package com.sdgakj.web.h5;

import com.google.common.collect.Maps;
import com.sdgakj.dto.FeedDTO;
import com.sdgakj.entity.Donation;
import com.sdgakj.entity.Order;
import com.sdgakj.entity.Person;
import com.sdgakj.entity.User;
import com.sdgakj.enums.EnumFeedType;
import com.sdgakj.rest.UserLoginRestController;
import com.sdgakj.service.feed.FeedService;
import com.sdgakj.service.order.OrderService;
import com.sdgakj.service.orderFlow.DonationService;
import com.sdgakj.service.person.PersonService;
import com.sdgakj.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/h5/user")
public class H5UserController {
	
	private static final String PAGE_SIZE = "10";
	
	private static Map<String,String> sortTypes =Maps.newLinkedHashMap();
	private static Logger logger = LoggerFactory.getLogger(UserLoginRestController.class);
	@Autowired
	private  OrderService orderService;
	@Autowired
	private PersonService personService;
	@Autowired
	private FeedService feedService;
	@Autowired
	private UserService userService;
	@Autowired
	private DonationService donationService;


	static {
		sortTypes.put("auto", "自动");
	}



    /**
     * 登陆页面
     * @return
     */
    @RequestMapping(value="/login",method=RequestMethod.GET)
    public String login(){
        //return "h5/user/wxuser_login";
        return "h5/user/user_login";
    }

	/**
	 * 个人页面
	 * @return
	 */
	@RequestMapping(value="/mzone",method=RequestMethod.GET)
	public String mzone( @RequestParam(value = "userId", required = false) Long id,Model model) {

	   	User user = userService.getUserById(id);
		if(user!=null){
       		model.addAttribute("user",user);
		}
		Person person=personService.getPersonByUserId(id);
		if(person!=null){
			model.addAttribute("person",person);
		}
		return "h5/user/myself";
	}

	/**
	 * 个人页面
	 * @return
	 */
	@RequestMapping(value="/tomzone",method=RequestMethod.GET)
	public String mzoneJsp() {
		return "h5/user/myself";
	}

	/**
	 * 娟赠
	 * @return
	 */
	@RequestMapping(value="/devote",method=RequestMethod.GET)
	public String devote(@RequestParam(value = "userId" ,required = false) Long id,
						 @RequestParam(value = "page", defaultValue = "1") int pageNumber,
						 @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
						 @RequestParam(value = "sortType", defaultValue = "auto") String sortType,
						 @RequestParam(value = "sortName", defaultValue = "auto") String sortName,
						 Model model){

	/*	Map<String, Object> searchParams1 =new HashMap<>();
		Page<User> user = userService.getfamilyfamouspById(searchParams1,pageNumber,pageSize,sortName,sortType);*/
		List<User> user=userService.getPointOredrByUserId();
		Map<String, Object> searchParams =new HashMap<>();
		Page<Donation> donations = donationService.getOrderByPages(searchParams,pageNumber,pageSize,sortName,sortType);
		model.addAttribute("user",user);
		model.addAttribute("donations",donations);
		return "h5/user/user-devote";
	}

	/**
	 * 设置
	 * @return
	 */
	@RequestMapping(value="/settings",method=RequestMethod.GET)
	public String settings(){
		return "h5/user/settings";
	}

	/**
	 * 意见反馈
	 * @return
	 */
	@RequestMapping(value="/sugestion",method=RequestMethod.GET)

	public String sugestion(){
		return "h5/user/sugestion";
	}
	/**
	 * 积分列表页
	 * @return
	 */
	@RequestMapping(value="/jifen",method=RequestMethod.GET)
	public String jifen(@RequestParam(value = "userId", required = false ) Long id,
						Model model){
		    User user = userService.getUserById(id);
		    model.addAttribute("user",user);
		return "h5/user/jifenList";
	}

	/**
     * 个人详情页面加载
     * */
	@RequestMapping(value = {"/personal"},method = {RequestMethod.GET})
	public String update(@RequestParam(value = "id",required = false) Long id,
						  @RequestParam(value = "personId",required = false) Long personId,
						  @RequestParam(value = "url",required = true)String url,
						  @RequestParam(value = "familyId",required = false,defaultValue = "0")Long familyId, Model model) {
		//当前用户信息
		if(familyId!=null && familyId!=0L){
			model.addAttribute("familyId",familyId);
		}
		Person person=null;
		if(id!=null && id!=0L) {
			 person= this.personService.getPersonByUserId(id);
		}
		if(person==null && personId!=null){
			person=this.personService.getPersonById(personId);
			if(person.getUser()!=null) {
				id = person.getUser().getId();
			}
		}
		model.addAttribute("person", person);
	/*
	 //	PersonDetail personDetail = this.personService.getPersonDetailById(id);
	 //	model.addAttribute("personDetail", personDetail);
	 //	PersonDetail personDetail = this.personService.getPersonDetailById(person.getId());*/
		//历程
		List<FeedDTO> historys = this.feedService.getMyList(id, EnumFeedType.HISTORY.getCode());
		model.addAttribute("historys", historys);
		//分享
		List<FeedDTO> shares = this.feedService.getMyList(id, EnumFeedType.SHARE.getCode());

		model.addAttribute("shares", shares);

		//判断用户是男女，
		if(person.getS().equals("M")){
			Person wife=personService.getPersonById(person.getUx());
			if(wife!=null) {
				model.addAttribute("ux", wife);
			}
		}else {
			Person vir=personService.getPersonById(person.getVir());
			if(vir!=null) {
				model.addAttribute("vir", vir);
			}
		}

		//父亲的信息 非空验证
		if(person.getF()!=null && person.getF()!=0){
		//	System.out.println("父亲id"+person.getF());
			Person fater=this.personService.getPersonById(person.getF());
			model.addAttribute("father",fater);
		}
		//母亲的信息
		if(person.getM()!=null && person.getM()!=0){
		//	System.out.println("母亲id"+person.getM());
			Person mother=this.personService.getPersonById(person.getM());
			model.addAttribute("mother",mother);
		}
		//兄弟姐妹 如果大小只有一个排除自己
		if(person.getF()!=null && person.getF()>0){
			List<Person> brother=this.personService.getPersonByFatherId(person.getF());
			List<Person> list=new ArrayList<>();
			if(brother!=null && brother.size()>0){
				if(brother.size()==1){
					if (brother.get(0).getId() == person.getId()) {
						System.out.println("除了自己以外！");
					} else {
						list.add(brother.get(0));
					}
				}else {
					for (Person p:brother) {
						if(!p.getId().equals(person.getId())){
							list.add(p);
						}
					}
				}
			}
			model.addAttribute("brother", list);
		}
		//孩子
		List<Person> children=this.personService.getPersonByFatherId(person.getId());
		model.addAttribute("children",children);
		if(url.equals("0")){
			model.addAttribute("delete","yes");
		}
		return "h5/user/personal";
	}

	/**
	 * 我的详情页
	 * */
	@RequestMapping(value="/personformation",method=RequestMethod.GET)
	public String persettings(@RequestParam(value = "id", required = true)Long id,Model model){
		Person person=personService.getPersonById(id);
		if(person!=null){
			model.addAttribute("person",person);
		}
		return "h5/user/personnalinformation";
	}
	/*@RequestMapping(value = "/testlogin")
	public String test(){
		return "/h5/user/login";
	}*/

	/**
	 * 捐赠详情
	 * */
	@RequestMapping(value = "/devoteDetail")
	public String getDevoteDetail(Long id,Model model){

		Donation devote=donationService.getDonationById(id);
		model.addAttribute("devote",devote);
		return  "h5/user/devote_details";
	}

/*
	//微信登录
	@RequestMapping("/weixinLogin")
	public String weixinLogin(Model model, String code, HttpServletRequest req, HttpServletResponse response){
		if (code != null) {
			logger.info("用户已同意微信授权请求");
			//Customer customer = loginByWeixinService.getWeixinInfo(code);
		*/
/*	if (customer != null) {
				this.login(customer, req.getSession(), req, response, "");
				// 如果微信登录没有绑定手机号码则提示用户进行绑定
				if (StringUtils.isBlank(customer.getMobilePhone())) {
					return "redirect:/wxBind";
				}
				return "redirect:"+indexPage;
			}*//*

		}
		return "/tologin";
	}
*/

}

